<?php
require_once '../model/userBmsDao.php';

// 显示所有用户信息
$_SESSION['users'] = get_all_users();
if (isset($_SESSION['users'])) {
  $users = $_SESSION['users'];
} else {
  $users = null;
}
// 判断是否是修改
if (isset($_GET['save']) && $_GET['save'] == '修改') {
  $uId = $_POST['uId'];
  $uPass = $_POST['uPass'];
  $gender = $_POST['gender'];

  // 头像处理
  if (isset($_FILES['head']) && $_FILES['head']['error'] == 0) {
    $ext = pathinfo($_FILES['head']['name'], PATHINFO_EXTENSION);
    $newName = time() . rand(1000, 9999) . '.' . $ext;
    move_uploaded_file($_FILES['head']['tmp_name'], "../../bbs/image/head/" . $newName);
    $head = $newName;
  } else {
    // 没有上传新头像，使用原头像
    $head = $_POST['origin_head'];
  }
  edit_user($uId, $uPass, $head, $gender);
  // 根据id判断是否是管理员
  if (isset($_SESSION['uId'])&&$_SESSION['uId']==$uId&&$_SESSION['role']=="user") {
    // 修改成功后跳转到用户管理页面
    header('Location:../views/indexBms.php?action=user');  
  }else{    
    header('Location:../views/indexBms.php?action=setting');  
  }
}
if (isset($_GET['del']) && $_GET['del'] == 'delete') {
  del_user($_GET['uId']);
  header('Location:../views/indexBms.php?action=user');  
}


?>